package com.ht.api.db.bean;

import javax.sql.DataSource;

import com.ht.api.db.Db;
import com.ht.api.db.DbConfig;
import com.ht.api.db.Entity;
import com.ht.api.db.StreamLoadConfig;
import com.ht.api.db.exception.HtException;
import com.ht.api.db.util.RedisUtil;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

/**
 * 登陆对象Bean
 * @author asq
 * @createTime 2025年3月22日 11:12:28
 */
@Data
@Builder
@AllArgsConstructor
public class SessionBean {
	private long loginId;
	private long tenantId;
	private DataSource dataSource;
	private StreamLoadConfig streamLoadConfig;
	private boolean isSysCommonCall;
	private String openid; 
	
	/**
	 * 获取登陆人ID字符串
	 * @return
	 */
	public String getLoginIdStr() {
		return String.valueOf(loginId);
	}
	
	/**
	 * 获取租户ID字符串
	 * @return
	 */
	public String getTenantIdStr() {
		return String.valueOf(tenantId);
	}

	/**
	 * 构造<b><系统数据库></b>LoginBean
	 * @return
	 */
	public static SessionBean createBySys() {
		return SessionBean.builder()
				.dataSource(DbConfig.useSysDb())
				.streamLoadConfig(DbConfig.useSysSLC())
				.build();
	}

	/**
	 * 构造<b><系统数据库></b>LoginBean
	 * @param loginId 登陆ID
	 * @return
	 */
	public static SessionBean createBySys(long loginId) {
		return SessionBean.builder()
				.loginId(loginId)
				.dataSource(DbConfig.useSysDb())
				.streamLoadConfig(DbConfig.useSysSLC())
				.build();
	}

	/**
	 * 构造<b><租户数据库></b>LoginBean
	 * @param loginId 登陆ID
	 * @return
	 */
	public static SessionBean createByLogin(long loginId) {
		SessionBean lb = SessionBean.builder().loginId(loginId).build();
		
		// 查询登陆用户的租户ID
		Entity login = Db.createSqlParser("sys_user").findById(loginId);
		if (login != null) {
			Long loginTenantId = login.getLong("tenant_id");
			if (loginTenantId != null) {
				lb.setTenantId(loginTenantId);
				lb.setDataSource(DbConfig.useTenantDb(loginTenantId));
				lb.setStreamLoadConfig(DbConfig.useTenantSLC(loginTenantId));
			}
		}
		
		return lb;
	}

	/**
	 * 构造<b><租户数据库></b>LoginBean
	 * @param tenantId 租户ID
	 * @return
	 */
	public static SessionBean createByTenant(long tenantId) {
		return SessionBean.builder()
				.tenantId(tenantId)
				.dataSource(DbConfig.useTenantDb(tenantId))
				.streamLoadConfig(DbConfig.useTenantSLC(tenantId))
				.build();
	}
	
	/**
	 * 构造<b><租户数据库></b>LoginBean
	 * @param host 域名
	 * @return
	 */
	public static SessionBean createByHost(String host) {
		return createByHost(host, null);
	}
	
	/**
	 * 构造<b><租户数据库></b>LoginBean
	 * @param host 域名
	 * @param openid 
	 * @return
	 */
	public static SessionBean createByHost(String host, String openid) {
		Entity tenant = RedisUtil.get("sys_tenant" + host);
    	if (tenant == null) {
    		throw new HtException("找不到host对应的数据源！");
    	}
		
    	long tenantId = tenant.getLong("main_id");
    	DataSource ds = DbConfig.useTenantDb(tenantId);
    	StreamLoadConfig slc = DbConfig.useTenantSLC(tenantId);
    	
    	// 创建SessionBean对象
		SessionBean me = SessionBean.builder()
				.tenantId(tenantId)
				.openid(openid)
				.dataSource(ds)
				.streamLoadConfig(slc)
				.build();
		
		return me;
	}
}
